import numpy as np
import platgo as pg


class Schwefel(pg.Problem):

    def __init__(self, D: int=20):
        self.name = 'Schwefel'
        self.type['single'], self.type['real'], self.type['large'], self.type['expensive'] = [True] * 4
        self.M = 1
        self.D = D
        lb = [-500] * D
        ub = [500] * D
        self.borders = np.array([lb, ub])
        super().__init__()

    def cal_obj(self, pop: pg.Population) -> None:
        decs = pop.decs
        pop.objv = np.array([-np.sum(decs*np.sin(np.sqrt(abs(decs))), 1)]).T

    def get_optimal(self) -> np.ndarray:
        return np.array([[0]])